package com.wanmait.travel.controller.manager;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.oreilly.servlet.MultipartRequest;
import com.oreilly.servlet.multipart.FileRenamePolicy;
import com.wanmait.travel.dao.ProvinceCityDAO;
import com.wanmait.travel.dao.ScenicDAO;
import com.wanmait.travel.vo.City;
import com.wanmait.travel.vo.Manager;
import com.wanmait.travel.vo.Pager;
import com.wanmait.travel.vo.Province;
import com.wanmait.travel.vo.Scenic;
import com.wanmait.travel.vo.ScenicTickitType;
import com.wanmait.travel.vo.Tickit;

@WebServlet("/ScenicController")
public class ScenicController extends HttpServlet {
	private ScenicDAO scenicDAO= ScenicDAO.getDAO();
	private ProvinceCityDAO provinceCityDAO=ProvinceCityDAO.getDAO();

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String action= request.getParameter("action");
		if (action == null) {
			response.sendRedirect("/Travel");
			return;
		}
		switch(action){
		case "add":
			this.add(request, response);
			break;
		case "delete":
			this.delete(request, response);
			break;
		case "save":
			this.save(request, response);
			break;
		case "edit":
			this.edit(request, response);
			break;
		case "list":
			this.list(request, response);
			break;
		case "searchFind":
			this.searchFind(request, response);
			break;
		case "cityFind":
			this.cityFind(request, response);
			break;
		case "provinceFind":
			this.provinceFind(request, response);
			break;
		case "province":
			this.province(request, response);
			break;
		case "city":
			this.city(request, response);
			break;
		case "recover":
			this.recover(request, response);
			break;
		case "provinceAll":
			this.provinceAll(request, response);
			break;
		case "cityAll":
			this.cityAll(request, response);
			break;		
		case "myShow":
			this.myShow(request, response);
			break;		
		case "scenicShow":
			this.scenicShow(request, response);
			break;		
		}
		}

	
	protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		Manager manager=(Manager)request.getSession().getAttribute("manager");
		String dir=this.getServletContext().getRealPath("/upload");
		MyFileRenamePolicy renamePolicy =new MyFileRenamePolicy();
		MultipartRequest req =new MultipartRequest(request,dir,1024*1024*20,"UTF-8",renamePolicy);
		
		
		String name=req.getParameter("name");
		String provinceIdStr =req.getParameter("provinceId");
		int provinceId=Integer.parseInt(provinceIdStr);
		String cityIdStr=req.getParameter("cityId");
		int cityId=Integer.parseInt(cityIdStr);
		String location=req.getParameter("location");
		String openTime=req.getParameter("openTime");
		String closeTime=req.getParameter("closeTime");
		String intro=req.getParameter("intro");
		String introPic=req.getParameter("introPic");
		String introVideo=req.getParameter("introVideo");
		
		
		Scenic scenic =new Scenic();
		scenic.setCityId(cityId);
		scenic.setCloseTime(closeTime);
		scenic.setIntro(intro);
		scenic.setIntroPic(introPic);
		scenic.setIntroVideo(introVideo);
		scenic.setLocation(location);
		scenic.setName(name);
		scenic.setOpenTime(openTime);
		scenic.setManagerId(manager.getId());
		scenic.setProvinceId(provinceId);
		
		Tickit tickitChild=null;
		//儿童票类型与价格
		String priceCStr=req.getParameter("priceChild");
		if(priceCStr.equals("整数或小数")||priceCStr.equals("")) {
			
		}else {
			double priceC=Double.parseDouble(priceCStr);
			String numCStr=req.getParameter("numChild");
			int numC=Integer.parseInt(numCStr);
			String tickitCStr=req.getParameter("tickitChild");
			int tickitC=Integer.parseInt(tickitCStr);
			//儿童票类型与价格
			tickitChild =new Tickit();
			tickitChild.setPrice(priceC);
			tickitChild.setNum(numC);
			tickitChild.setTickitTypeId(tickitC);
		}
		
				
		Tickit tickitMan=null;
		String priceMStr=req.getParameter("priceMan");
		System.out.println(priceMStr);
		if(priceMStr.equals("整数或小数")||priceMStr.equals("")) {
			
		}else {
		//成人票类型与价格
		double priceM=Double.parseDouble(priceMStr);
		String numMStr=req.getParameter("numMan");
		int numM=Integer.parseInt(numMStr);
		String tickitMStr=req.getParameter("tickitMan");
		int tickitM=Integer.parseInt(tickitMStr);
		//成人票类型与价格
		tickitMan =new Tickit();
		tickitMan.setPrice(priceM);
		tickitMan.setNum(numM);
		tickitMan.setTickitTypeId(tickitM);
		}

		//老年票类型与价格
		Tickit tickitOld=null;
		String priceOStr=req.getParameter("priceOld");
		if(priceOStr.equals("整数或小数")||priceOStr.equals("")) {
			
		}else {
		tickitOld =new Tickit();
		double priceOld=Double.parseDouble(priceOStr);
		String numOStr=req.getParameter("numOld");
		int numOld=Integer.parseInt(numOStr);
		String tickitOStr=req.getParameter("tickitOld");
		int tickitO=Integer.parseInt(tickitOStr);
		tickitOld.setPrice(priceOld);
		tickitOld.setNum(numOld);
		tickitOld.setTickitTypeId(tickitO);
		}
		
		
		int identity=scenicDAO.insert(scenic,manager);
		if(identity!=0)
		{
		scenicDAO.tickitAdd(identity,tickitMan,tickitOld,tickitChild);
		response.sendRedirect("/Travel/ScenicController?action=list");
		}else {
		response.sendRedirect("/Travel/scenic/add.jsp");
		}
	}
	
	protected void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String path=request.getServletContext().getContextPath();
		String scenicIdStr=request.getParameter("id");
		int scenicId=Integer.parseInt(scenicIdStr);
		scenicDAO.delete(scenicId);
		response.sendRedirect(path+"/ScenicController?action=list");
	}
	
	protected void recover(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String path=request.getServletContext().getContextPath();
		String scenicIdStr=request.getParameter("id");
		int scenicId=Integer.parseInt(scenicIdStr);
	
		scenicDAO.recover(scenicId);
		response.sendRedirect(path+"/ScenicController?action=list");
	}
	
	protected void edit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {		
		String scenicIdStr=request.getParameter("id");
		int scenicId=Integer.parseInt(scenicIdStr);
		List<Scenic> scenics=scenicDAO.findById(scenicId);
		ScenicTickitType stt=scenicDAO.findTickitTypeAll(scenics);
		request.setAttribute("stt", stt);
		request.getRequestDispatcher("/scenic/edit.jsp").forward(request, response);
	}
	
	
	protected void save(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String path=request.getServletContext().getContextPath();
		request.setCharacterEncoding("UTF-8");
		Manager manager=(Manager)request.getSession().getAttribute("manager");
		String dir=this.getServletContext().getRealPath("/upload");
		MyFileRenamePolicy renamePolicy =new MyFileRenamePolicy();
		MultipartRequest req =new MultipartRequest(request,dir,1024*1024*20,"UTF-8",renamePolicy);
		
		String scenicIdStr=req.getParameter("id");
		int scenicId=Integer.parseInt(scenicIdStr);
		String name=req.getParameter("name");
		String provinceIdStr =req.getParameter("provinceId");
		int provinceId=Integer.parseInt(provinceIdStr);
		String cityIdStr=req.getParameter("cityId");
		int cityId=Integer.parseInt(cityIdStr);
		String location=req.getParameter("location");
		String openTime=req.getParameter("openTime");
		String closeTime=req.getParameter("closeTime");
		String intro=req.getParameter("intro");
		String introPic=req.getParameter("introPic");
		String introVideo=req.getParameter("introVideo");
		
		
		Scenic scenic =new Scenic();
		scenic.setId(scenicId);
		scenic.setCityId(cityId);
		scenic.setCloseTime(closeTime);
		scenic.setIntro(intro);
		scenic.setIntroPic(introPic);
		scenic.setIntroVideo(introVideo);
		scenic.setLocation(location);
		scenic.setName(name);
		scenic.setOpenTime(openTime);
		scenic.setManagerId(manager.getId());
		scenic.setProvinceId(provinceId);
		
		
		Tickit tickitChild=null;
		//儿童票类型与价格
		String priceCStr=req.getParameter("priceChild");
		if(priceCStr.equals("整数或小数")||priceCStr.equals("")) {
		}else {
			double priceC=Double.parseDouble(priceCStr);
			String numCStr=req.getParameter("numChild");
			int numC=Integer.parseInt(numCStr);
			String tickitCStr=req.getParameter("tickitChild");
			int tickitC=Integer.parseInt(tickitCStr);
			//儿童票类型与价格
			tickitChild =new Tickit();
			tickitChild.setPrice(priceC);
			tickitChild.setNum(numC);
			tickitChild.setTickitTypeId(tickitC);
			tickitChild.setScenicId(scenicId);
		}
		
				
		Tickit tickitMan=null;
		String priceMStr=req.getParameter("priceMan");
		if(priceMStr.equals("整数或小数")||priceMStr.equals("")) {
		}else {
		//成人票类型与价格
		double priceM=Double.parseDouble(priceMStr);
		String numMStr=req.getParameter("numMan");
		int numM=Integer.parseInt(numMStr);
		String tickitMStr=req.getParameter("tickitMan");
		int tickitM=Integer.parseInt(tickitMStr);
		//成人票类型与价格
		tickitMan =new Tickit();
		tickitMan.setPrice(priceM);
		tickitMan.setNum(numM);
		tickitMan.setTickitTypeId(tickitM);
		tickitMan.setScenicId(scenicId);
		}

		//老年票类型与价格
		Tickit tickitOld=null;
		String priceOStr=req.getParameter("priceOld");
		if(priceOStr.equals("整数或小数")||priceOStr.equals("")) {
		}else {
		tickitOld =new Tickit();
		double priceOld=Double.parseDouble(priceOStr);
		String numOStr=req.getParameter("numOld");
		int numOld=Integer.parseInt(numOStr);
		String tickitOStr=req.getParameter("tickitOld");
		int tickitO=Integer.parseInt(tickitOStr);
		tickitOld.setPrice(priceOld);
		tickitOld.setNum(numOld);
		tickitOld.setTickitTypeId(tickitO);
		tickitOld.setScenicId(scenicId);
		}
		
		
		scenicDAO.update(scenic);
		if(tickitChild!=null) {
			scenicDAO.updateTickit(tickitChild);
		}
		if(tickitOld!=null) {
			scenicDAO.updateTickit(tickitOld);
		}
		if(tickitMan!=null) {
			scenicDAO.updateTickit(tickitMan);
		}
		response.sendRedirect(path+"/ScenicController?action=list");
	}
	
	protected void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String pargeNumStr =request.getParameter("pageNum");
		Pager pager=Pager.init(pargeNumStr);
		scenicDAO.findAllSvince(pager);
		request.setAttribute("scenicAll", pager);
		request.getRequestDispatcher("/scenic/list.jsp").forward(request, response);
	}
	
	//搜索发现景点
	/**
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void searchFind(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String search=request.getParameter("search");
		request.getSession().setAttribute("search", search);
		String pargeNumStr =request.getParameter("pageNum");
		Pager pager=Pager.init(pargeNumStr);
		int count=scenicDAO.getSearchDataCount(search);
		pager.setRowCount(count);
		Pager pagers=scenicDAO.findBySearch(pager,search);
		request.setAttribute("searchShow", pagers);
		request.getRequestDispatcher("/scenic/searchShow.jsp").forward(request, response);
	}
	
	protected void cityFind(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String cityIdStr=request.getParameter("cityId");
		int cityId=Integer.parseInt(cityIdStr);
		List<Scenic> scenics=scenicDAO.findCitySearch(cityId);
		request.setAttribute("searchScenic", scenics);
		request.getRequestDispatcher("/ajax/Dsearchlist.jsp").forward(request, response);
	}


	protected void cityAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String cityNumStr=request.getParameter("pageNum");
		Pager cityPager=Pager.init(cityNumStr);
		scenicDAO.findCityAll(cityPager);
		request.setAttribute("cityAll", cityPager);
		request.getRequestDispatcher("/ajax/cShow.jsp").forward(request, response);
	}

	
	protected void provinceFind(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String provinceStr=request.getParameter("provinceId");
		int provinceId=Integer.parseInt(provinceStr);
		List<Scenic> scenics=scenicDAO.findProvinceSearch(provinceId);
		request.setAttribute("searchScenic", scenics);
		request.getRequestDispatcher("/ajax/Dsearchlist.jsp").forward(request, response);
	}
	
	
	protected void provinceAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String provinceNumStr=request.getParameter("pageNum");
		Pager pager=Pager.init(provinceNumStr);
		scenicDAO.findProvinceALL(pager);
		request.setAttribute("provinceAll", pager);
		request.getRequestDispatcher("/ajax/jShow.jsp").forward(request, response);
	}

	
	//返回省的集合
	protected void province(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		List<Province> provinces=provinceCityDAO.provinceSearch();
		request.setAttribute("provinces", provinces);
		request.getRequestDispatcher("/ajax/provinces.jsp").forward(request, response);
	}
	
	
	//返回市的集合
	protected void city(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String pId=request.getParameter("id");
		int provinceId=Integer.parseInt(pId);

		List<City> citys=provinceCityDAO.cityAllSearch(provinceId);
		request.setAttribute("citys", citys);
		request.getRequestDispatcher("/ajax/city.jsp").forward(request, response);
	}
	
	protected void myShow(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String pId=request.getParameter("managerId");
		int managerId=Integer.parseInt(pId);
		List<Scenic> scenics=scenicDAO.managerScenicShow(managerId);
		request.setAttribute("scenicAll", scenics);
		request.getRequestDispatcher("/scenic/my.jsp").forward(request, response);
	}
	
	protected void scenicShow(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String pId=request.getParameter("scenicId");
		int scenicId=Integer.parseInt(pId);
		List<Scenic> scenicShow=scenicDAO.findById(scenicId);
		request.setAttribute("scenicShow", scenicShow);
		request.getRequestDispatcher("/manager/scenicShow.jsp").forward(request, response);
	}
	
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}
	
	
	class MyFileRenamePolicy implements FileRenamePolicy{
		@Override
		public File rename(File file) {
			String oldName=file.getName();
			String ext = oldName.substring(oldName.lastIndexOf(","));
			String filename = UUID.randomUUID().toString()+ext;
			File nFile = new File(file.getParentFile(),filename);
			return nFile;
		}
	}
}